﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Utility.NodeGraphs;

namespace Utility.BehaviourTree
{
	public class VisualizerNodeGraph : TreeNodeGraph
	{
		#region build graph
		public static VisualizerNodeGraph Build (BTRootNode rootNode)
		{
			if (rootNode == null)
			{
				return null;
			}

			var graph = CreateInstance<VisualizerNodeGraph> ();
			var v_root = graph.BuildVisualizerNode (rootNode);
			v_root.isRoot = true;
			v_root.position = Vector2.zero;

			return graph;
		}

		VisualizerNode BuildVisualizerNode (BTNode node)
		{
			var v_node = AddNewNode<VisualizerNode> ();
			v_node.btNode = node;
			v_node.name = node.GetType ().Name;

			if (node is CompositeNode)
			{
				foreach (var child in (node as CompositeNode).ChildNodes)
				{
					var v_child = BuildVisualizerNode (child);
					v_node.AddChildPort ().ConnectTo (v_child.ParentPort);
				}
			}
			else if (node is DecoratorNode)
			{
				var v_child = BuildVisualizerNode ((node as DecoratorNode).ChildNode);
				v_node.AddChildPort ().ConnectTo (v_child.ParentPort);
			}

			return v_node;
		}
		#endregion

	}
}